package com.lenovo.server.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import org.hibernate.Query;
import com.lenovo.server.vote.bean.Option;
import com.lenovo.server.vote.bean.Topic;
import com.lenovo.server.vote.bean.VoteUser;

public class HibDB extends DB{
	
	public static List<Option>  getAllOption(){
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Option");
		List<Option> lists = (List<Option>)q.list();
		return lists;
	}
	public static Configuration cfg = new AnnotationConfiguration();
	public static SessionFactory sf = cfg.configure().buildSessionFactory();
	public static void save(Object obj){
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		session.save(obj);
		session.getTransaction().commit();
	}
	
	public static List<VoteUser> getAllUser(){
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from User");
		List<VoteUser> lists = (List<VoteUser>)q.list();
		return lists;
	}
	
	
	public static List<Topic> queryTopic(){
		Session session = sf.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Topic");
		List<Topic> lists = (List<Topic>)q.list();
		
		for(Topic c : lists) {
			try {
				System.err.println("ddd" + c.getName());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		session.getTransaction().commit();
		
		return lists;
	}
	
	
	public static void main(String[] args){

		
	}
	
}
